<?php
namespace Pay\Controller;
use Admin\Controller\InitController;
class SpendController extends InitController{
	public function _initialize(){
		parent::_initialize();
		$this->db = D('PaySpend');
	}
	public function init(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize  = 20;
		$sqlMap = "";
		if(submitcheck('dosubmit')){
			$username = isset($_POST['username']) && trim($_POST['username']) ? trim($_POST['username']) : '';
			$op = isset($_POST['op']) && trim($_POST['op']) ? trim($_POST['op']) : '';
			$user_type = isset($_POST['user_type']) && intval($_POST['user_type']) ? intval($_POST['user_type']) : '';
			$op_type = isset($_POST['op_type']) && intval($_POST['op_type']) ? intval($_POST['op_type']) : '';
			$type = isset($_POST['type']) && intval($_POST['type']) ? intval($_POST['type']) : '';
			$endtime = isset($_POST['endtime'])  &&  trim($_POST['endtime']) ? strtotime(trim($_POST['endtime'])) : '';
			$starttime = isset($_POST['starttime']) && trim($_POST['starttime']) ? strtotime(trim($_POST['starttime'])) : '';
			
			if (!empty($starttime) && empty($endtime)) {
				$endtime = SYS_TIME;
			}
			//时间格式错误	
			if (!empty($starttime) && !empty($endtime) && $endtime < $starttime) {
				$this->error(L('wrong_time_over_time_to_time_less_than'));
			}
			
			if(!empty($username) && $user_type == 1){
				$sqlMap .= $sqlMap? " AND `username` = '$username'" : " `username` = '$username'";
			}
			if (!empty($username) && $user_type == 2) {
				$sqlMap .= $sqlMap ? " AND `userid` = '$username'" : " `userid` = '$username'";
			}
				
			if (!empty($starttime)) {
				$sqlMap .= $sqlMap ? " AND `creat_at` BETWEEN '$starttime' AND '$endtime' " : " `creat_at` BETWEEN '$starttime' AND '$endtime' ";
			}
				
			if (!empty($op) && $op_type == 1) {
				$sqlMap .= $sqlMap ? " AND `op_username` = '$op' " : " `op_username` = '$op' ";
			} elseif (!empty($op) && $op_type == 2) {
				$sqlMap .= $sqlMap ? " AND `op_userid` = '$op' " : " `op_userid` = '$op' ";
			}
			//消费类型
			if (!empty($type)) {
				$sqlMap .= $sqlMap ? " AND `type` = '$type' " : " `type` = '$type'";
			}
		}
		$count = $this->db->where($sqlMap)->count();
		$lists = $this->db->where($sqlMap)->page($pagecurr,$pagesize)->select();
		$pages = page($count,$pagesize);
		//echo $this->db->getLastSql();		
		$form = new \Common\Library\form();
		$format = new \Common\Library\format();
		include $this->admin_tpl('spend_list');
		
	}
}
?>